﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using RoadTeam.BLL;
using RoadTeam.Pub;

/// <summary>
///ThreadWriteFile 的摘要说明
/// </summary>
public class ThreadWriteFile
{
    public ThreadWriteFile() { }

    public ThreadWriteFile(string Paths, string oFileName, byte[] buffer, String BusninessID,string RtSysPath)
    {
        this.fielName = oFileName;
        this.buffer = buffer;
        this.paths = Paths;
        this.busninessID = BusninessID;
        this.rtSysPath = RtSysPath;
    }

    private string rtSysPath;
    public string RtSysPath
    {
        get { return rtSysPath; }
        set { rtSysPath = value; }
    }


    private string fielName;
    public string FielName
    {
        get { return fielName; }
        set { fielName = value; }
    }
    private byte[] buffer;
    public byte[] Buffer
    {
        get { return buffer; }
        set { buffer = value; }
    }

    private string paths;
    public string Paths
    {
        get { return paths; }
        set { paths = value; }
    }
    private string busninessID;
    public string BusninessID
    {
        get { return busninessID; }
        set { busninessID = value; }
    }

    private string staffName;
    public string StaffName
    {
        get { return staffName; }
        set { staffName = value; }
    }

    public delegate void ThreadFinished(String BusninessID, String RtSysPath, String Paths, String FileName);
    public event ThreadFinished threadFinished;

    public void write()
    {
        bool write = true;
        while (write)
        {
            try
            {

                if (!Directory.Exists(Paths))//判断是否存在        
                    Directory.CreateDirectory(Paths);//创建新路径
                using (FileStream fs = new FileStream(Paths + "\\" + FielName, FileMode.Create))
                {
                    fs.Write(buffer, 0, buffer.Length);
                    write = false;
                    fs.Flush();
                    fs.Close();
                    if (!write && threadFinished != null)
                    {
                        CsValidateRoomPicBll bll = new CsValidateRoomPicBll();
                        bll.Insert(BusninessID, RtSysPath, Paths, FielName);
                        //判断文件是否已经保存在服务器中
                        FileOperate.FileExist(Paths, FielName);
                    }
                        //threadFinished(BusninessID,RtSysPath ,Paths , FielName);
                }
            }
            catch { }
        }
    }
}
